package cn.enilu.flash.api.controller.shop;

import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.core.BussinessLog;
import cn.enilu.flash.bean.entity.shop.Goods;
import cn.enilu.flash.bean.entity.shop.Label;
import cn.enilu.flash.bean.entity.shop.LabelGoods;
import cn.enilu.flash.bean.exception.ApplicationException;
import cn.enilu.flash.bean.exception.ApplicationExceptionEnum;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.bean.vo.shop.LabelVO;
import cn.enilu.flash.service.shop.LabelGoodsService;
import cn.enilu.flash.service.shop.LabelService;
import cn.enilu.flash.utils.factory.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shop/label")
public class LabelController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private LabelService labelService;
    @Autowired
    private LabelGoodsService labelGoodsService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Object list(){
        Page<Label> page = new PageFactory<Label>().defaultPage();
//        page.addFilter("name", SearchFilter.Operator.LIKE,name);
        page.setSort(Sort.by(Sort.Direction.DESC,"recommend"));
        page = labelService.queryPage(page);
        page.getRecords().stream().forEach(l->{
            List<LabelGoods> labelGoodsList = labelGoodsService.findByLabelId(l.getId());
            l.setLabelGoodsList(labelGoodsList);
        });
        return Rets.success(page);
    }

    @RequestMapping(method = RequestMethod.POST)
    @BussinessLog(value = "新增或修改标签", key = "name")
    public Object save(@RequestBody Label label){
        if(null == label.getId()){
            labelService.insert(label);
        }else {
            labelService.update(label);
        }
        return Rets.success();
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @BussinessLog(value = "删除", key = "id")
    public Object remove(@RequestParam("id") Long id){
        if (id == null) {
            throw new ApplicationException(ApplicationExceptionEnum.REQUEST_NULL);
        }
        labelService.deleteById(id);
        return Rets.success();
    }

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    @BussinessLog(value = "获取全部标签")
    public Object getAll(){
        List<Label> labelList = labelService.queryAllSort();
        return Rets.success(labelList);
    }
}
